package com.aibili.contoller.api;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aibili.common.JPushUtil;
import com.aibili.pojo.BannerInfo;
import com.aibili.pojo.EventInfo;
import com.aibili.pojo.EventType;
import com.aibili.pojo.EventUser;
import com.aibili.pojo.NoticeInfo;
import com.aibili.pojo.UserDevice;
import com.aibili.pojo.UserInfo;
import com.aibili.service.EventService;
import com.aibili.service.NoticeInfoService;
import com.aibili.service.UserDeviceService;
import com.alibaba.fastjson.JSON;
import com.daoshun.common.CommonUtils;
import com.daoshun.common.DsConstant;
import com.daoshun.exception.CustomException;

@Controller
@RequestMapping("/api/event")
public class ApiEventContoller extends ApiBaseContoller {
	@Resource
	private EventService eventService;

	@Resource
	private NoticeInfoService noticeInfoService;

	@Resource
	private UserDeviceService userDeviceService;

	/**
	 * 活动banner
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws CustomException
	 */
	@ResponseBody
	@RequestMapping(value = "/banner", produces = "application/json; charset=UTF-8")
	public String banner(HttpServletRequest request, HttpServletResponse response) throws CustomException {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			List<BannerInfo> list = eventService.banner();
			map.put("banner_list", list);
			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return JSON.toJSONString(map);
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}

	/**
	 * 发布活动
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/publish", produces = "application/json; charset=UTF-8")
	public String publish(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			String user_id = request.getParameter("user_id");
			String event_name = request.getParameter("event_name");
			String event_type = request.getParameter("event_type");
			String place = request.getParameter("place");
			String intro = request.getParameter("intro");
			String event_time = request.getParameter("event_time");
			CommonUtils.validateEmpty(user_id);
			CommonUtils.validateEmpty(event_name);
			CommonUtils.validateEmpty(event_type);
			CommonUtils.validateEmpty(place);
			CommonUtils.validateEmpty(intro);
			CommonUtils.validateEmpty(event_time);
			Date eve_time = CommonUtils.getDateFormat(event_time, "yyyy-MM-dd");
			EventInfo event = new EventInfo();
			Date create_time = new Date();
			event.setCreate_time(create_time);
			UserInfo user = eventService.getuser(CommonUtils.parseLong(user_id, 0));
			if (user != null) {
				event.setUser(user);
				event.setEvent_name(event_name);
				event.setEvent_type(event_type);
				event.setPlace(place);
				event.setIntro(intro);
				event.setEvent_time(eve_time);
				eventService.publish(event);
				map.putAll(DsConstant.SUCCESS_RESULT_MAP);
				return JSON.toJSONString(map);
			} else {
				return DsConstant.NO_USER;
			}
		} catch (CustomException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}

	/**
	 * 活动列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list", produces = "application/json; charset=UTF-8")
	public String list(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			String type = request.getParameter("type");
			CommonUtils.validateEmpty(type);
			String page = request.getParameter("page");
			if (CommonUtils.isEmptyString(page)) {
				page = "1";
			}
			int pages = CommonUtils.parseInt(page, 0);
			List<EventInfo> list = eventService.list(type, pages);
			map.put("event_list", list);
			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return toJson(map);
		} catch (CustomException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}

	/**
	 * 热门活动
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws CustomException
	 */
	@ResponseBody
	@RequestMapping(value = "/hot", produces = "application/json; charset=UTF-8")
	public String hot(HttpServletRequest request, HttpServletResponse response) throws CustomException {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			List<EventType> type_list = eventService.hot();
			map.put("type_list", type_list);
			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return toJson(map);
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}

	/**
	 * 活动详情
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/detail", produces = "application/json; charset=UTF-8")
	public String detail(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			String event_id = request.getParameter("event_id");
			String user_id = request.getParameter("user_id");
			CommonUtils.validateEmpty(event_id);
			EventInfo event = eventService.detail(CommonUtils.parseLong(event_id, 0));
			map.put("event", event);

			// 参与活动的总人数
			long count = eventService.countEventUserByEventid(CommonUtils.parseLong(event_id, 0));
			map.put("join_count", count);

			int join_type = 0;
			if (!CommonUtils.isEmptyString(user_id)) {
				EventUser eventuser = eventService.getEventUserByEventidUserid(CommonUtils.parseLong(event_id, 0),
						CommonUtils.parseLong(user_id, 0));
				if (eventuser != null) {
					join_type = 1;
				}
			}
			map.put("join_type", join_type);
			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return toJson(map);
		} catch (CustomException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}

	/**
	 * 
	 * @Description 参加活动的人
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/detailinfo", produces = "application/json; charset=UTF-8")
	public String detailinfo(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			String event_id = request.getParameter("event_id");
			String page = request.getParameter("page");
			CommonUtils.validateEmpty(event_id);
			List<EventUser> infos = eventService.pageByEventid(CommonUtils.parseLong(event_id, 0),
					CommonUtils.parseInt(page, 1));
			map.put("info_list", infos);
			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return toJson(map);
		} catch (CustomException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}

	/**
	 * 参加活动
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/join", produces = "application/json; charset=UTF-8")
	public String join(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			String user_id = request.getParameter("user_id");
			String event_id = request.getParameter("event_id");
			CommonUtils.validateEmpty(user_id);
			CommonUtils.validateEmpty(event_id);
			long uid = CommonUtils.parseLong(user_id, 0);
			long eid = CommonUtils.parseLong(event_id, 0);
			Date join_time = new Date();
			EventUser eventuser = new EventUser();

			EventUser com = eventService.getEventUserByEventidUserid(eid, uid);
			if (com != null) {
				return DsConstant.EVENT_EXIST;
			} else {
				UserInfo user = eventService.getuser(uid);
				if (user != null) {
					eventuser.setEvent_id(eid);
					eventuser.setJoin_time(join_time);
					eventuser.setUser(user);
					eventuser.setGift_id(0);
					eventuser.setGift_name("");
					eventService.join(eventuser);

					// 参与活动的总人数
					long count = eventService.countEventUserByEventid(eid);
					map.put("join_count", count);

					// 消息推送
					NoticeInfo notice = new NoticeInfo();
					// 获得弄人的id
					// 通过活动去找
					EventInfo info = eventService.getEventInfoById(eid);
					if (info != null) {
						notice.setUser_id(info.getUser().getId());
					}
					notice.setPortrait(user.getPortrait()); // 评论人的头像
					notice.setTitle("有人参加了你的活动！");
					notice.setContent(user.getUser_name() + "参加了你的活动！");
					notice.setPublish_time(new Date());

					noticeInfoService.addNoticeInfo(notice);

					// 发送推送
					// 找到设备信息
					JPushUtil jpush = new JPushUtil();
					List<UserDevice> devices = userDeviceService.getByUserid(info.getUser().getId());
					for (UserDevice userDevice : devices) {
						if (userDevice.getType() == 0) {
							jpush.sendSingleAndroidPush(userDevice.getClient_id(), user.getUser_name() + "参加了你的活动！");
						} else {
							jpush.sendSingleIOSPush(userDevice.getClient_id(), user.getUser_name() + "参加了你的活动！", null);
						}
					}

					map.putAll(DsConstant.SUCCESS_RESULT_MAP);
					return JSON.toJSONString(map);
				} else {
					return DsConstant.NO_USER;
				}
			}

		} catch (CustomException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}

	/**
	 * 参加活动的用户
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/users", produces = "application/json; charset=UTF-8")
	public String users(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			String event_id = request.getParameter("event_id");
			CommonUtils.validateEmpty(event_id);
			long eid = CommonUtils.parseLong(event_id, 0);
			List<UserInfo> userlist = eventService.users(eid);
			map.put("user_list", userlist);
			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return toJson(map);
		} catch (CustomException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}

	/**
	 * 发布活动的模板信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws CustomException
	 */
	@ResponseBody
	@RequestMapping(value = "/info", produces = "application/json; charset=UTF-8")
	public String info(HttpServletRequest request, HttpServletResponse response) throws CustomException {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			List<EventType> list = eventService.info();
			map.put("type_list", list);
			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return toJson(map);
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}

	/**
	 * 我的活动列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/mine", produces = "application/json; charset=UTF-8")
	public String mine(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			String user_id = request.getParameter("user_id");
			String page = request.getParameter("page");
			CommonUtils.validateEmpty(user_id);
			if (CommonUtils.isEmptyString(page)) {
				page = "1";
			}
			long userid = CommonUtils.parseLong(user_id, 0);
			int pages = CommonUtils.parseInt(page, 0);
			List<EventInfo> list = eventService.mine(userid, pages);
			map.put("event_list", list);
			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return toJson(map);
		} catch (CustomException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}

	@ResponseBody
	@RequestMapping(value = "/joined", produces = "application/json; charset=UTF-8")
	public String joined(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			String user_id = request.getParameter("user_id");
			String page = request.getParameter("page");
			CommonUtils.validateEmpty(user_id);
			if (CommonUtils.isEmptyString(page)) {
				page = "1";
			}
			long userid = CommonUtils.parseLong(user_id, 0);
			int pages = CommonUtils.parseInt(page, 1);

			List<EventInfo> list = eventService.joined(userid, pages);

			map.put("event_list", list);
			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return toJson(map);
		} catch (CustomException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}

}
